<?php
class SsoPanel_ApplicationController extends Ext_Controller_SecureAction
{
    public function listAction()
    {
       	$data = new Ext_Model_Application();
       	$this->view->paginator = new Ext_Paginator( 
       		$data->select(), 
       		$this->getRequest()->getParam('page') 
       	);
    }
    
    public function createAction()
    {
        $request = $this->getRequest();
        $form = new SsoPanel_Form_Application();

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($request->getPost())) {
                $model = new Ext_Model_Application();
                $row = $model->createRow(  $form->getValues()  );
                $row->save();
	            $this->_redirect( 	        
		        	$this->view->url( 
			        	array(
			        		'controller'=>'application' 
			        		,'action'=>'list'
			        	)
			        	, "default"
					)
		         );
            }
        }
        $this->view->form = $form;
    }

    public function updateAction()
    {
        $request = $this->getRequest();
        $form = new SsoPanel_Form_Application();
		$model = new Ext_Model_Application();
		$data = $model->find( $request->id )->current()	;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid( $request->getPost() ) ) {
                $data->name = $form->getValue( 'name' );
                $data->save();
	            $this->_redirect( 	        
		        	$this->view->url( 
			        	array(
			        		'controller'=>'application' 
			        		,'action'=>'list'
			        	)
			        	, "default"
					)
		         );
            }
        } else {
        	$form->populate( $data->toArray() );
        }
        $this->view->form = $form;
    }
    
    public function deleteAction()
    {
		$model = new Ext_Model_Application();
		$row = $model->find( $this->getRequest()->id )->current();
		if( $row != null ) {
			$row->locked =  ( $row->locked == 'T' ) ? 'F' : 'T';
			$row->end_date = ( $row->locked == 'T' ) ? Ext_Db_Date::now() : null ;
			$row->save();
		}
		$this->_redirect( 	        
        	$this->view->url( 
	        	array(
	        		'controller'=>'application' 
	        		,'action'=>'list'
	        	)
	        	, "default"
			)
         );
    }
}
